using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace MONA.Modules.Marketing.BLL
{
    public partial class WorkItem
    {
        #region Private members.

        protected int _ID;
        protected int _ServiceID;
        protected int _ActionID;
        protected string _Stage = string.Empty;
        protected DateTime _LastExecutedTime;
        protected bool _IsExecuted;
        protected bool _IsRepeat;
        protected int _PriorityID;
        protected DateTime _DueDate;
        protected string _Summary = string.Empty;
        protected int _AssignedToUserID;
        protected DateTime _LastUpdatedDate;
        protected int _LastUpdatedByUserID;
        protected DateTime _CreatedDate;
        protected int _CreatedByUserID;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public int ServiceID
        {
            set { this._ServiceID = value; }
            get { return this._ServiceID; }
        }

        public int ActionID
        {
            set { this._ActionID = value; }
            get { return this._ActionID; }
        }

        public string Stage
        {
            set { this._Stage = value; }
            get { return this._Stage; }
        }

        public DateTime LastExecutedTime
        {
            set { this._LastExecutedTime = value; }
            get { return this._LastExecutedTime; }
        }

        public bool IsExecuted
        {
            set { this._IsExecuted = value; }
            get { return this._IsExecuted; }
        }

        public bool IsRepeat
        {
            set { this._IsRepeat = value; }
            get { return this._IsRepeat; }
        }

        public int PriorityID
        {
            set { this._PriorityID = value; }
            get { return this._PriorityID; }
        }

        public DateTime DueDate
        {
            set { this._DueDate = value; }
            get { return this._DueDate; }
        }

        public string Summary
        {
            set { this._Summary = value; }
            get { return this._Summary; }
        }

        public int AssignedToUserID
        {
            set { this._AssignedToUserID = value; }
            get { return this._AssignedToUserID; }
        }

        public DateTime LastUpdatedDate
        {
            set { this._LastUpdatedDate = value; }
            get { return this._LastUpdatedDate; }
        }

        public int LastUpdatedByUserID
        {
            set { this._LastUpdatedByUserID = value; }
            get { return this._LastUpdatedByUserID; }
        }

        public DateTime CreatedDate
        {
            set { this._CreatedDate = value; }
            get { return this._CreatedDate; }
        }

        public int CreatedByUserID
        {
            set { this._CreatedByUserID = value; }
            get { return this._CreatedByUserID; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static WorkItem Load(int iD)
        {
            string spName = "[dbo].p_MARKETING_WorkItem_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            WorkItem entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new WorkItem();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ServiceID"))) entity.ServiceID = reader.GetInt32(reader.GetOrdinal("ServiceID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ActionID"))) entity.ActionID = reader.GetInt32(reader.GetOrdinal("ActionID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Stage"))) entity.Stage = reader.GetString(reader.GetOrdinal("Stage"));
                if (!reader.IsDBNull(reader.GetOrdinal("LastExecutedTime"))) entity.LastExecutedTime = reader.GetDateTime(reader.GetOrdinal("LastExecutedTime"));
                if (!reader.IsDBNull(reader.GetOrdinal("IsExecuted"))) entity.IsExecuted = reader.GetBoolean(reader.GetOrdinal("IsExecuted"));
                if (!reader.IsDBNull(reader.GetOrdinal("IsRepeat"))) entity.IsRepeat = reader.GetBoolean(reader.GetOrdinal("IsRepeat"));
                if (!reader.IsDBNull(reader.GetOrdinal("PriorityID"))) entity.PriorityID = reader.GetInt32(reader.GetOrdinal("PriorityID"));
                if (!reader.IsDBNull(reader.GetOrdinal("DueDate"))) entity.DueDate = reader.GetDateTime(reader.GetOrdinal("DueDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) entity.Summary = reader.GetString(reader.GetOrdinal("Summary"));
                if (!reader.IsDBNull(reader.GetOrdinal("AssignedToUserID"))) entity.AssignedToUserID = reader.GetInt32(reader.GetOrdinal("AssignedToUserID"));
                if (!reader.IsDBNull(reader.GetOrdinal("LastUpdatedDate"))) entity.LastUpdatedDate = reader.GetDateTime(reader.GetOrdinal("LastUpdatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("LastUpdatedByUserID"))) entity.LastUpdatedByUserID = reader.GetInt32(reader.GetOrdinal("LastUpdatedByUserID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedByUserID"))) entity.CreatedByUserID = reader.GetInt32(reader.GetOrdinal("CreatedByUserID"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_ServiceID(int serviceID)
        {
            string spName = "[dbo].p_MARKETING_WorkItem_SelectBy_ServiceID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ServiceID", SqlDbType.Int, serviceID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------
        public static DataSet SelectBy_ActionID(int actionID)
        {
            string spName = "[dbo].p_MARKETING_WorkItem_SelectBy_ActionID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ActionID", SqlDbType.Int, actionID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_MARKETING_WorkItem_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_MARKETING_WorkItem_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_WorkItem_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_WorkItem_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(int serviceID, int actionID, string stage, DateTime lastExecutedTime, bool isExecuted, bool isRepeat, int priorityID, DateTime dueDate, string summary, int assignedToUserID, DateTime lastUpdatedDate, int lastUpdatedByUserID, DateTime createdDate, int createdByUserID)
        {
            WorkItem entity = new WorkItem();
            entity.ServiceID = serviceID;
            entity.ActionID = actionID;
            entity.Stage = stage;
            entity.LastExecutedTime = lastExecutedTime;
            entity.IsExecuted = isExecuted;
            entity.IsRepeat = isRepeat;
            entity.PriorityID = priorityID;
            entity.DueDate = dueDate;
            entity.Summary = summary;
            entity.AssignedToUserID = assignedToUserID;
            entity.LastUpdatedDate = lastUpdatedDate;
            entity.LastUpdatedByUserID = lastUpdatedByUserID;
            entity.CreatedDate = createdDate;
            entity.CreatedByUserID = createdByUserID;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_WorkItem_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@ServiceID", SqlDbType.Int, this._ServiceID);
            db.AddInParameter(dbCommand, "@ActionID", SqlDbType.Int, this._ActionID);
            db.AddInParameter(dbCommand, "@Stage", SqlDbType.NVarChar, this._Stage);
            db.AddInParameter(dbCommand, "@LastExecutedTime", SqlDbType.DateTime, this._LastExecutedTime);
            db.AddInParameter(dbCommand, "@IsExecuted", SqlDbType.Bit, this._IsExecuted);
            db.AddInParameter(dbCommand, "@IsRepeat", SqlDbType.Bit, this._IsRepeat);
            db.AddInParameter(dbCommand, "@PriorityID", SqlDbType.Int, this._PriorityID);
            db.AddInParameter(dbCommand, "@DueDate", SqlDbType.DateTime, this._DueDate);
            db.AddInParameter(dbCommand, "@Summary", SqlDbType.NVarChar, this._Summary);
            db.AddInParameter(dbCommand, "@AssignedToUserID", SqlDbType.Int, this._AssignedToUserID);
            db.AddInParameter(dbCommand, "@LastUpdatedDate", SqlDbType.DateTime, this._LastUpdatedDate);
            db.AddInParameter(dbCommand, "@LastUpdatedByUserID", SqlDbType.Int, this._LastUpdatedByUserID);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, this._CreatedDate);
            db.AddInParameter(dbCommand, "@CreatedByUserID", SqlDbType.Int, this._CreatedByUserID);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, int serviceID, int actionID, string stage, DateTime lastExecutedTime, bool isExecuted, bool isRepeat, int priorityID, DateTime dueDate, string summary, int assignedToUserID, DateTime lastUpdatedDate, int lastUpdatedByUserID, DateTime createdDate, int createdByUserID)
        {
            WorkItem entity = new WorkItem();
            entity.ID = iD;
            entity.ServiceID = serviceID;
            entity.ActionID = actionID;
            entity.Stage = stage;
            entity.LastExecutedTime = lastExecutedTime;
            entity.IsExecuted = isExecuted;
            entity.IsRepeat = isRepeat;
            entity.PriorityID = priorityID;
            entity.DueDate = dueDate;
            entity.Summary = summary;
            entity.AssignedToUserID = assignedToUserID;
            entity.LastUpdatedDate = lastUpdatedDate;
            entity.LastUpdatedByUserID = lastUpdatedByUserID;
            entity.CreatedDate = createdDate;
            entity.CreatedByUserID = createdByUserID;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_WorkItem_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@ServiceID", SqlDbType.Int, this._ServiceID);
            db.AddInParameter(dbCommand, "@ActionID", SqlDbType.Int, this._ActionID);
            db.AddInParameter(dbCommand, "@Stage", SqlDbType.NVarChar, this._Stage);
            db.AddInParameter(dbCommand, "@LastExecutedTime", SqlDbType.DateTime, this._LastExecutedTime);
            db.AddInParameter(dbCommand, "@IsExecuted", SqlDbType.Bit, this._IsExecuted);
            db.AddInParameter(dbCommand, "@IsRepeat", SqlDbType.Bit, this._IsRepeat);
            db.AddInParameter(dbCommand, "@PriorityID", SqlDbType.Int, this._PriorityID);
            db.AddInParameter(dbCommand, "@DueDate", SqlDbType.DateTime, this._DueDate);
            db.AddInParameter(dbCommand, "@Summary", SqlDbType.NVarChar, this._Summary);
            db.AddInParameter(dbCommand, "@AssignedToUserID", SqlDbType.Int, this._AssignedToUserID);
            db.AddInParameter(dbCommand, "@LastUpdatedDate", SqlDbType.DateTime, this._LastUpdatedDate);
            db.AddInParameter(dbCommand, "@LastUpdatedByUserID", SqlDbType.Int, this._LastUpdatedByUserID);
            db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, this._CreatedDate);
            db.AddInParameter(dbCommand, "@CreatedByUserID", SqlDbType.Int, this._CreatedByUserID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            WorkItem entity = new WorkItem();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_WorkItem_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}